package com.lfy.fyadmin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lfy.fyadmin.entity.Group;
import com.lfy.fyadmin.entity.UserGroup;
import com.lfy.fyadmin.entity.GroupPermission;
import com.lfy.fyadmin.service.GroupService;
import com.lfy.fyadmin.service.UserGroupService;
import com.lfy.fyadmin.service.GroupPermissionService;
import com.lfy.fyadmin.mapper.GroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author 53409
* @description 针对表【group(用户分组表)】的数据库操作Service实现
* @createDate 2025-07-23 21:51:27
*/
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group>
    implements GroupService{

    @Autowired
    private UserGroupService userGroupService;
    
    @Autowired
    private GroupPermissionService groupPermissionService;

    @Override
    public boolean updateSelective(Group group) {
        return this.baseMapper.updateSelective(group) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteGroupWithRelations(Long groupId) {
        try {
            // 1. 删除用户分组关联关系
            userGroupService.lambdaUpdate()
                    .eq(UserGroup::getGroupId, groupId)
                    .remove();

            // 2. 删除分组权限关联关系
            groupPermissionService.lambdaUpdate()
                    .eq(GroupPermission::getGroupId, groupId)
                    .remove();

            // 3. 删除分组本身
            return this.removeById(groupId);
        } catch (Exception e) {
            throw new RuntimeException("删除分组及其关联资源失败", e);
        }
    }
}




